package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.ConditionVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.insurance.enums.ConditionEnum;
import com.itheima.sfbx.insurance.mapper.ConditionMapper;
import com.itheima.sfbx.insurance.pojo.*;
import com.itheima.sfbx.insurance.service.ICategoryConditionService;
import com.itheima.sfbx.insurance.service.IConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * @Description：筛选项服务实现类
 */
@Slf4j
@Service
public class ConditionServiceImpl extends ServiceImpl<ConditionMapper, Condition> implements IConditionService {


    private LambdaQueryWrapper<Condition> queryWrapper(ConditionVO conditionVO){
        LambdaQueryWrapper<Condition> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(conditionVO.getConditionKey()),Condition::getConditionKey,conditionVO.getConditionKey())
                .like(StrUtil.isNotBlank(conditionVO.getConditionKeyName()), Condition::getConditionKeyName, conditionVO.getConditionKeyName())
                .eq(StrUtil.isNotBlank(conditionVO.getDataState()), Condition::getDataState, conditionVO.getDataState());
        return lqw;
    }
    @Override
    public Page<ConditionVO> findPage(ConditionVO conditionVO, int pageNum, int pageSize) {
        if (ObjectUtil.isEmpty(conditionVO)){
            log.error("系数项参数为空!,{}",conditionVO);
            throw new ProjectException(ConditionEnum.PAGE_FAIL);
        }
        LambdaQueryWrapper<Condition> lqw = queryWrapper(conditionVO);
        Page<Condition> page = this.page(new Page<>(pageNum, pageSize), lqw);
        return BeanConv.toPage(page, ConditionVO.class);
    }

    @Override
    public ConditionVO findById(String conditionId) {
        //TODO
        return null;
    }

    @Override
    @Transactional
    public ConditionVO save(ConditionVO conditionVO) {
        Integer count = lambdaQuery()
                .eq(Condition::getConditionKey, conditionVO.getConditionKey())
//                .eq(Coefficent::getDataState, coefficentVO.getDataState())
                .count();
        if (count>0){
            log.error("该系数项已存在:{}",conditionVO.getConditionKey());
            throw new ProjectException(CoefficentEnum.SAVE_FAIL);
        }
        Condition bean = BeanConv.toBean(conditionVO, Condition.class);
        this.save(bean);
        return BeanConv.toBean(bean, ConditionVO.class);
    }
    @Autowired
    private ICategoryConditionService categoryConditionService;
    @Autowired
    private IInsuranceConditionService insuranceConditionService;
    @Override
    @Transactional
    public Boolean update(ConditionVO conditionVO) {
        Condition condition = this.getById(conditionVO.getId());
        if (ObjectUtil.isEmpty(condition)){
            log.error("找不到该系数项,无法修改:{}",conditionVO.getId());
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        Integer count = lambdaQuery()
                .eq(Condition::getConditionKey, conditionVO.getConditionKey())
                .ne(Condition::getId, conditionVO.getId())
                .count();
        if (count>0){
            log.error("系数项key已存在,无法修改:{}",conditionVO.getId());
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        Integer count1 = categoryConditionService.lambdaQuery().eq(CategoryCondition::getConditionKey,conditionVO.getConditionKey()).count();
        Integer count2 = insuranceConditionService.lambdaQuery().eq(InsuranceCondition::getConditionKey,conditionVO.getConditionKey()).count();
        if (count1>0||count2>0){
            log.error("有关联,无法修改:{}",conditionVO.getConditionKey());
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        boolean flag = updateById(BeanConv.toBean(conditionVO, Condition.class));
        return flag;
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        for (String checkedId : checkedIds) {
            Condition condition = this.getById(checkedId);
            if (ObjectUtil.isEmpty(condition)){
                log.error("找不到该系数项,无法删除:{}",checkedId);
                throw new ProjectException(ConditionEnum.DEL_FAIL);
            }
            Integer count1 = categoryConditionService.lambdaQuery().eq(CategoryCondition::getConditionKey,condition.getConditionKey()).count();
            Integer count2 = insuranceConditionService.lambdaQuery().eq(InsuranceCondition::getConditionKey,condition.getConditionKey()).count();
            if (count1>0||count2>0){
                log.error("有关联,无法修改:{}",condition.getConditionKey());
                throw new ProjectException(ConditionEnum.UPDATE_FAIL);
            }
            removeById(checkedId);
        }
        return true;
    }

    @Override
    public List<ConditionVO> findList(ConditionVO conditionVO) {
        LambdaQueryWrapper<Condition> lqw = queryWrapper(conditionVO);
        List<Condition> list = this.list(lqw);
        return BeanConv.toBeanList(list,ConditionVO.class);
    }

    @Override
    public List<ConditionVO> findInConditionKeys(List<String> conditionKeys) {
        //TODO
        return null;
    }
}
